package com.smile.service.task;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.smile.service.handler.AbstractServicesHandler;
import com.smile.service.entity.services.*;
import lombok.Data;
import lombok.ToString;
import org.eclipse.paho.client.mqttv3.MqttException;

import java.util.UUID;

@Data
@ToString
public class SpeakServicesExecutor {

    private Integer psdkIndex = 2;

    private String data;

    private String gateway;


    public static SpeakServicesExecutor create() {
        return new SpeakServicesExecutor();
    }

    public SpeakServicesExecutor speakerPlayVolumeSet(Integer playVolume) {
        SpeakerPlayVolumeSetData setData = new SpeakerPlayVolumeSetData();
        setData.setPlayVolume(playVolume);
        setData.setPsdkIndex(psdkIndex);
        BaseServicesEntity<SpeakerPlayVolumeSetData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(setData);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("speaker_play_volume_set");
        SpeakServicesExecutor speakServicesExecutor = new SpeakServicesExecutor();
        speakServicesExecutor.setData(JSONObject.toJSONString(speakServicesExecutor));
        return speakServicesExecutor;
    }

    public SpeakServicesExecutor speakerPlayModeSet(Integer playMode) {
        SpeakerPlayModeSetData speakerPlayModeSetData = new SpeakerPlayModeSetData();
        speakerPlayModeSetData.setPlayMode(playMode);
        speakerPlayModeSetData.setPsdkIndex(psdkIndex);
        BaseServicesEntity<SpeakerPlayModeSetData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(speakerPlayModeSetData);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("speaker_play_mode_set");
        SpeakServicesExecutor speakServicesExecutor = new SpeakServicesExecutor();
        speakServicesExecutor.setData(JSONObject.toJSONString(speakServicesExecutor));
        return speakServicesExecutor;
    }

    public SpeakServicesExecutor gateway(String gateway) {
        SpeakServicesExecutor gatewayExecutor = this;
        gatewayExecutor.setGateway(gateway);
        return gatewayExecutor;
    }


    public void execute() throws MqttException {
        AbstractServicesHandler bean = SpringUtil.getBean(AbstractServicesHandler.class);
        bean.process(data,gateway);
    }


    public SpeakServicesExecutor speakerTtsPlayStart(String name, String text, String md5) {
        SpeakerTtsPlayStartData.Tts tts = new SpeakerTtsPlayStartData.Tts();
        tts.setName(name);
        tts.setText(text);
        tts.setMd5(md5);
        SpeakerTtsPlayStartData speakerTtsPlayStartData = new SpeakerTtsPlayStartData();
        speakerTtsPlayStartData.setTts(tts);
        speakerTtsPlayStartData.setPsdkIndex(psdkIndex);
        BaseServicesEntity<SpeakerTtsPlayStartData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(speakerTtsPlayStartData);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("speaker_tts_play_start");
        SpeakServicesExecutor speakServicesExecutor = new SpeakServicesExecutor();
        speakServicesExecutor.setData(JSONObject.toJSONString(speakServicesExecutor));
        return speakServicesExecutor;
    }

    public SpeakServicesExecutor speakerAudioPlayStart(
            String name, String url, String md5
    ) {
        SpeakerAudioPlayStartData speakerAudioPlayStartData = new SpeakerAudioPlayStartData();
        SpeakerAudioPlayStartData.File file = new SpeakerAudioPlayStartData.File();
        file.setName(name);
        file.setUrl(url);
        file.setMd5(md5);
        file.setFormat("pcm");
        speakerAudioPlayStartData.setFile(file);
        BaseServicesEntity<SpeakerAudioPlayStartData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(speakerAudioPlayStartData);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("speaker_audio_play_start");


        SpeakServicesExecutor speakServicesExecutor = new SpeakServicesExecutor();
        speakServicesExecutor.setData(JSONObject.toJSONString(speakServicesExecutor));
        return speakServicesExecutor;
    }
}
